package com.authority.framework.web.controller;

import com.authority.framework.base.util.DateUtils;
import com.authority.framework.base.util.ShiroUtils;
import com.authority.framework.base.util.laydata.LayDatas;
import com.authority.framework.base.util.laydata.PageUtils;
import com.authority.framework.base.util.laydata.ResultModel;
import com.authority.framework.base.util.laydata.ResultTools;
import com.authority.framework.log.Log;
import com.authority.framework.log.enums.BusinessType;
import com.authority.framework.web.domain.SysDictDataEntity;
import com.authority.framework.web.domain.SysDictTypeEntity;
import com.authority.framework.web.domain.SysUserEntity;
import com.authority.framework.web.service.SysDictDataService;
import com.authority.framework.web.service.SysDictTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: authority2
 * @description: 字典类型数据控制层
 * @author: Mr.Yan
 * @create: 2019-01-09 23:21
 **/
@Controller
@RequestMapping(value = "sysdictdataCon")
public class SysDictDataController {

    private static final Logger log = LoggerFactory.getLogger(SysDictDataController.class);

    @Autowired
    private SysDictDataService sysDictDataService;

    private Map<String, Object> paramMap;//参数

    private Map<String, Object> resultMap;//返回值

    /**
     * @Author: Mr.Yan
     * @Date: 2019-01-09 23:21
     * @MethodName: list
     * @Description: 获取字典类型分页数据
     * @Param: [request, response, pageUtils]
     * @return: com.authority.framework.base.util.laydata.LayDatas
     */
    @RequestMapping("/list")
    @ResponseBody
    public LayDatas list(HttpServletRequest request, HttpServletResponse response,
                         PageUtils pageUtils) {
        return sysDictDataService.pageLayInfo("system/SysDictDataSql", SysDictDataEntity.class, pageUtils);
    }

    /**
     * @Author: Mr.Yan
     * @Date: 2019-01-09 23:21
     * @MethodName: insert
     * @Description: 字典类型具体数据管理添加
     * @Param: [request, response, ajaxMap]
     * @return: com.authority.framework.base.util.laydata.ResultModel
     */
    @Log(title = "字典类型具体数据管理", businessType = BusinessType.INSERT)
    @RequestMapping("/insert")
    @ResponseBody
    public ResultModel insert(HttpServletRequest request, HttpServletResponse response,
                              @RequestParam Map<String, Object> ajaxMap) {
        try {
            //  获取登陆用户信息
            SysUserEntity userEntity = ShiroUtils.getSubjectUser();
            ajaxMap.put("createBy", userEntity.getUserName());
            //  获取当前时间
            String time = DateUtils.getTime();
            ajaxMap.put("createTime", time);
            Integer insert = sysDictDataService.insert(SysDictDataEntity.class, ajaxMap);
            if (insert > 0) {
                return ResultTools.result(0, "", null);
            } else {
                return ResultTools.result(404, "添加字典类型具体数据信息失败", null);
            }
        } catch (Exception e) {
            log.error("添加字典类型具体数据信息出现错误" + e.getMessage());
            return ResultTools.result(404, "添加字典类型具体数据信息出现错误", null);
        }
    }

    /**
     * @Author: Mr.Yan
     * @Date: 2019/1/10
     * @MethodName: openDeptUpdate
     * @Description: 打开字典修改之前查看具体值
     * @Param: [request, response, deptId]
     * @return: org.springframework.web.servlet.ModelAndView
     */
    @ResponseBody
    @RequestMapping(value = "/openDictDataUpdate")
    public ModelAndView openDictDataUpdate(HttpServletRequest request, HttpServletResponse response,
                                       @RequestParam(name = "dictCode", required = false) Integer dictCode) {
        ModelAndView mv = new ModelAndView();
        try {
            SysDictDataEntity dictDataEntity = sysDictDataService.findByPrimaryKey(SysDictDataEntity.class, dictCode);
            mv.addObject("dictData", dictDataEntity);
        } catch (Exception e) {
            log.error("打开字典类型具体修改框出现错误" + e.getMessage());
            e.printStackTrace();
        }
        mv.setViewName("web/system/dict/dictData/dictData_update");
        return mv;
    }

    /**
     * @Description: 更新字典具体数据信息
     * @Param:
     * @return:
     * @Author: Mr.Yan
     * @Date: 2018/12/7
     */
    @Log(title = "字典类型具体数据管理", businessType = BusinessType.UPDATE)
    @ResponseBody
    @RequestMapping(value = "/update")
    public ResultModel update(HttpServletRequest request, HttpServletResponse response,
                              @RequestParam Map<String, Object> ajaxMap) {
        try {
            //  获取当前时间
            String time = DateUtils.getTime();
            //  获取登陆用户信息
            SysUserEntity userEntity = ShiroUtils.getSubjectUser();
            ajaxMap.put("updateTime", time);
            ajaxMap.put("updateBy", userEntity.getUserName());
            Integer update = sysDictDataService.update(SysDictDataEntity.class, ajaxMap);
            if (update > 0) {
                return ResultTools.result(0, "", null);
            } else {
                return ResultTools.result(404, "更新字典类型具体数据信息失败", null);
            }
        } catch (Exception e) {
            log.error("更新字典类型具体数据信息出现错误" + e.getMessage());
            return ResultTools.result(404, "更新字典类型具体数据信息出现错误", null);
        }
    }

    /**
     * 删除 字典类型具体数据管理
     *
     * @param request
     * @param response
     * @param dictIds
     * @return
     */
    @Log(title = "字典类型具体数据管理", businessType = BusinessType.DELETE)
    @ResponseBody
    @RequestMapping(value = "/deleteDictData")
    public ResultModel deleteDictData(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(name = "dictCodes", required = false) String dictCodes) {
        try {
            String[] dictCodeArr = dictCodes.split(",");
            for (String dictCode : dictCodeArr) {
                paramMap = new HashMap<>();
                //  获取当前时间
                String time = DateUtils.getTime();
                //  获取登陆用户信息
                SysUserEntity userEntity = ShiroUtils.getSubjectUser();
                paramMap.put("updateTime", time);
                paramMap.put("updateBy", userEntity.getUserName());
                paramMap.put("delFlag", 2);
                paramMap.put("dictCode", dictCode);
                Integer update = sysDictDataService.update(SysDictDataEntity.class, paramMap);
                if (update < 1) {
                    return ResultTools.result(404, "删除字典类型具体数据信息失败", null);
                }
            }
            return ResultTools.result(0, "", null);
        } catch (Exception e) {
            log.error("删除字典类型具体数据信息出现未知错误" + e.getMessage());
            return ResultTools.result(404, "删除字典类型具体数据信息出现未知错误", null);
        }
    }
}
